(function(){

YAHOO.pendular.RepositoryDataTable = function(config){
    if (!config) {
        config = {};
    }
    YAHOO.lang.augmentObject(config, {
        target: "pendular-search-results"
    });
    this.config = config;
    this.onResultLinkClicked = new YAHOO.util.CustomEvent("resultLinkClicked");
};

YAHOO.extend(YAHOO.pendular.RepositoryDataTable, Object, {
    renderResult: function(response) {
        var rows = response.results, numRows = rows.length, row, i=0,
            el = YAHOO.util.Dom.get(this.config.target),
            html = "";
        if (!el){
            return;
        }
        for (i=0; i<numRows; i++){
            html += this.renderRow(rows[i], i);
        }
        el.innerHTML = html;
    },
    renderLink: function(row, i){
        var link, url, clickHandler;
        url = row.isDirectory ? "#" : row.url;        
        clickHandler =  "YAHOO.pendular.app.repositoryDataTable.onResultLinkClicked.fire(this);"+
                        "return false;"
        ;
        link = "<a class=\"pendular-result-link\"" +
                " tabindex=\"" + i + "\"" + 
                " title=\"" + row.key + "\"" + 
                " onclick=\"" + clickHandler + "\"" + 
                " href=\"" + YAHOO.lang.escapeHTML(url) + "\">" +
                row.localizedName +
                "</a>";
        return link;
    },
    renderRow: function(row, i){
        var link, description, favIcon;
        //favicon
        favIcon = "<span class=\"pendular-favicon\">&#160;&#160;&#160;&#160;</span>";
        //Link
        link = this.renderLink(row, i);
        //Description
        desc = YAHOO.lang.escapeHTML(row.description);
        desc = "<p class=\"pendular-result-desc\"" +
                " title=\"" + desc + "\"" +
                ">" + desc + "</p>";            
        
        return "<div" +
                " class=\"pendular-result pendular-result-type-" +
                (row.isDirectory ? "directory" : row.extension) +
                "\">" +
                favIcon + 
                link + 
                desc +
                "</div>"
                ;
    }
});

})();
